"""全局函数"""

import hashlib, os
from uuid import uuid4
from sqlalchemy import String
from .constant import DATABASE


# 创建唯一id
CREATE_ID = lambda: str(uuid4())
# MD5加密函数
ENCRYPTION = lambda x: hashlib.md5(x.encode("utf-8")).hexdigest()


def __SQL():
    """
    程序中用到的所有SQL
    使用文件的形式
    """

    class SQLITE3:
        def ALL_TABLES():
            """获取所有表"""
            return """SELECT name FROM sqlite_master WHERE type='table';"""

        def ALL_FILEDS(table_name):
            """获取表内所有字段名称（无数据）"""
            return f"""PRAGMA table_info({table_name});"""

        def ALTER_FIELD(table_name, column_name):
            """新增字段"""
            return f"ALTER TABLE {table_name} ADD COLUMN {column_name} VARCHAR(50);"

    class MYSQL:
        def ALL_DATABASES():
            """获取所有数据库"""
            return """SHOW DATABASES;"""

        def USE(database_name):
            """选中库"""
            return f"""USE {database_name}"""

        def ALL_TABLES():
            """查看所有的表"""
            return """SHOW TABLES;"""

    if DATABASE.DATABASE_TYPE == "mysql":
        return MYSQL
    elif DATABASE.DATABASE_TYPE == "sqlite":
        return SQLITE3


SQL = __SQL()


def GUESS_LANGUAGE(filename):
    """增强版语言猜测函数"""
    ext_map = {
        # 核心Web开发语言
        ".js": "javascript",
        ".ts": "typescript",
        ".html": "html",
        ".css": "css",
        ".json": "json",
        ".xml": "xml",
        ".yml": "yaml",
        ".yaml": "yaml",
        # 主流编程语言
        ".py": "python",
        ".java": "java",
        ".cs": "csharp",
        ".cpp": "cpp",
        ".h": "cpp",
        ".go": "go",
        ".rb": "ruby",
        ".php": "php",
        ".swift": "swift",
        ".rs": "rust",
        ".kt": "kotlin",
        ".scala": "scala",
        ".dart": "dart",
        # 脚本语言
        ".sh": "shell",
        ".ps1": "powershell",
        ".lua": "lua",
        ".pl": "perl",
        ".r": "r",
        # 标记语言
        ".md": "markdown",
        ".hbs": "handlebars",
        ".twig": "twig",
        # 样式预处理器
        ".less": "less",
        ".scss": "scss",
        ".sass": "sass",
        ".styl": "stylus",
        # 数据库相关
        ".sql": "sql",
        ".pgsql": "pgsql",
        ".mysql": "mysql",
        ".plsql": "plsql",
        # 配置/模板文件
        "dockerfile": "dockerfile",
        ".ini": "ini",
        ".properties": "properties",
        ".toml": "toml",
        # 特殊用途语言
        ".graphql": "graphql",
        ".sol": "solidity",
        ".vb": "vb",
        ".coffee": "coffeescript",
        ".clj": "clojure",
        ".ex": "elixir",
        ".erl": "erlang",
        ".fs": "fsharp",
        ".hs": "haskell",
        ".jl": "julia",
        ".m": "objective-c",
        ".ml": "ocaml",
        ".pas": "pascal",
        ".rkt": "racket",
        ".scm": "scheme",
        ".tcl": "tcl",
        # 特殊文件格式
        ".log": "log",
    }

    # 处理无后缀文件
    if "." not in filename:
        return "plaintext"

    # 处理Dockerfile等特殊文件名
    if filename.lower() == "dockerfile":
        return "dockerfile"

    _, ext = os.path.splitext(filename.lower())
    return ext_map.get(ext, "plaintext")  # 默认返回纯文本类型
